package com.topvision.jack.uploadlibrary.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.io.IOException;

/**
 * Created by gushenjie on 17/12/24.
 */

public class UploadUtils {

    public static final String ACTION_NETWORK = "ACTION_NETWORK";
    public static final String ACTION_NETWORK_FAILED = "ACTION_NETWORK_FAILED";

    /**
     * ping百度來判断是否能上网
     *
     * @author jack
     * create at 2016/11/29 16:31
     */
    public static final void ping(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = null;
                try {
                    String ip = "www.baidu.com";// ping 的地址，可以换成任何一种可靠的外网
                    Process p = Runtime.getRuntime().exec(
                            "ping -c 1 -w 100 " + ip);// ping网址1次
                    // ping的状态
                    int status = p.waitFor();
                    if (status == 0) {
                        result = "success";
                        context.sendBroadcast(new Intent(ACTION_NETWORK));
                    } else {
                        result = "failed";
                        context.sendBroadcast(new Intent(ACTION_NETWORK_FAILED));
                    }
                } catch (IOException e) {
                    result = "IOException";
                } catch (InterruptedException e) {
                    result = "InterruptedException";
                } finally {
                    Log.d("----result---", "result = " + result);
                }
            }
        }).start();

    }
}
